package com.barbeque.mall.controller.mallmanagement;

import com.barbeque.mall.anno.Log;
import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.order.*;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.service.mallmanagement.OrderService;
import com.barbeque.mall.util.OrderConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ycc
 * @date 2022/07/15 19:57:03
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    //GET请求 URL: http://localhost:8083/admin/order/channel
    @RequestMapping(value = "channel",method = RequestMethod.GET)
    public BaseRespVo channel(){
        List<ChannelVo> channelVos = orderService.channel();
        return BaseRespVo.ok(channelVos);
    }

    //GET请求 URL: http://localhost:8083/admin/order/list?page=1&limit=20&sort=add_time&order=desc&start=&end=s
    @RequiresPermissions("admin:order:list")
    @GetMapping("list")
    public BaseRespVo list(MarketOrder order, BasePageInfo pageInfo,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end,
                           Short[] orderStatusArray){
        //TODO:查询订单信息
        CommonData<MarketOrder> list = orderService.list(order,pageInfo,start,end,orderStatusArray);
        return BaseRespVo.ok(list);
    }

    //GET请求 http://localhost:8083/admin/order/detail?id=1
    @RequiresPermissions("admin:order:read")
    @GetMapping("detail")
    public BaseRespVo detail(Integer id){
        DetailVo detailVo = orderService.detail(id);
        return BaseRespVo.ok(detailVo);
    }

    //POST /admin/order/ship
    @RequiresPermissions("admin:order:ship")
    @Log(action = "发货",type = 2)
    @PostMapping("ship")
    public BaseRespVo ship(@RequestBody Map map){
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");
        int orderId = (int) map.get("orderId");
        if (StringUtils.isEmpty(shipChannel) || StringUtils.isEmpty(shipSn)) {
            return BaseRespVo.invalidData("请输入快递公司及编号");
        }



        int affectedRows = orderService.ship(map);

        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok("ok");
    }


    //POST http://localhost:8083/admin/order/refund
    @RequiresPermissions("admin:order:refund")
    @Log(action = "退款",type = 2)
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody Map map){
        int affectedRows = orderService.refund(map);
        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok("ok");
    }

    //POST http://localhost:8083/admin/order/delete
    @RequiresPermissions("admin:order:delete")
    @Log(action = "订单删除",type = 2)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        int orderId = (int) map.get("orderId");
        //订单id数据不合法
        if (orderId <= 0) {
            return BaseRespVo.invalidData();
        }

        int affectedRows = orderService.delete(orderId);
        //未删除成功提示错误信息
        if (affectedRows==0) {
            return BaseRespVo.noDeleted();
        }
        return BaseRespVo.ok("成功");
    }
}
